package Screens;

import Controllers.HdChamadosJpaController;
import Controllers.HdEquipamentoJpaController;
import Controllers.HdTipoProblemaJpaController;
import Entities.HdChamados;
import Entities.HdEquipamento;
import Entities.HdTipoProblema;
import Util.Constantes;
import Util.EMFConection;
import Util.StatusChamado;
import Util.UserLogado;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Ferrari
 */
public class CadastroChamado extends javax.swing.JFrame {

    /**
     * Creates new form CadastroChamado
     */
    public CadastroChamado() {
        initComponents();
        
        HdTipoProblemaJpaController tipoProblemaJpaController = new HdTipoProblemaJpaController(EMFConection.getInstance());
        List<HdTipoProblema> list = tipoProblemaJpaController.findHdTipoProblemaEntities();
        for (HdTipoProblema tipoProblema : list) {
            cbTipoProblema.addItem(tipoProblema.getDeTipoProblema());
        }
        
        HdEquipamentoJpaController equipamentoJpaController = new HdEquipamentoJpaController(EMFConection.getInstance());
        List<HdEquipamento> listEquipamento = equipamentoJpaController.findHdEquipamentoEntities();
        for (HdEquipamento equipamento : listEquipamento) {
            cbEquipamento.addItem(equipamento.getDeEquipamento());
        }
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbDeChamado = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taDeProblema = new javax.swing.JTextArea();
        lbEquipamento = new javax.swing.JLabel();
        cbEquipamento = new javax.swing.JComboBox();
        cbTipoProblema = new javax.swing.JComboBox();
        lbTipoProblema = new javax.swing.JLabel();
        btCancelar = new javax.swing.JToggleButton();
        btCadastrar = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Help Desk");

        lbDeChamado.setText("Descricao do problema:");

        taDeProblema.setColumns(20);
        taDeProblema.setRows(5);
        jScrollPane1.setViewportView(taDeProblema);

        lbEquipamento.setText("Equipamento:");

        lbTipoProblema.setText("Tiop de problema:");

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        btCadastrar.setText("Cadastrar");
        btCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCadastrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbDeChamado)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lbEquipamento)
                                .addGap(0, 118, Short.MAX_VALUE))
                            .addComponent(cbEquipamento, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbTipoProblema, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbTipoProblema)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbDeChamado)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbTipoProblema)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbTipoProblema, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbEquipamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lbEquipamento))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCancelar)
                    .addComponent(btCadastrar))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-410)/2, (screenSize.height-290)/2, 410, 290);
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCadastrarActionPerformed
        HdChamadosJpaController chamadosJpaController = new HdChamadosJpaController(EMFConection.getInstance());
        HdTipoProblemaJpaController problemaJpaController = new HdTipoProblemaJpaController(EMFConection.getInstance());
        HdEquipamentoJpaController equipamentoJpaController = new HdEquipamentoJpaController(EMFConection.getInstance());
        
        HdChamados hdChamado = new HdChamados();
        
        if (taDeProblema.getText().isEmpty() || taDeProblema.getText() == null) {
            JOptionPane.showMessageDialog(null, Constantes.CAMPO_DE_PROBLEMA_EM_BRANCO);
        }else{
            hdChamado.setDeChamado(taDeProblema.getText());                    
            hdChamado.setCoTipoProblema(problemaJpaController.findHdTipoProblema(cbTipoProblema.getSelectedIndex()+1));
            hdChamado.setCoEquipamento(equipamentoJpaController.findHdEquipamento(cbEquipamento.getSelectedIndex()+1));
            hdChamado.setDtCriacaoChamado(new Date());
            hdChamado.setDtInicioSuporte(null);
            hdChamado.setDtFinalizacaoSuporte(null);
            hdChamado.setCoUserResp(UserLogado.getInstance());
            hdChamado.setCoUserSuporte(null);
            hdChamado.setStChamado(StatusChamado.NOVO.getCodigo());
            this.dispose();
            try{
                chamadosJpaController.create(hdChamado);
                JOptionPane.showMessageDialog(null, Constantes.DADOS_INSERIDOS, Constantes.SUCESSO, JOptionPane.INFORMATION_MESSAGE);
            }catch(Exception e){
                JOptionPane.showMessageDialog(null, Constantes.ERRO_BANCO, Constantes.ERRO, JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_btCadastrarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btCadastrar;
    private javax.swing.JToggleButton btCancelar;
    private javax.swing.JComboBox cbEquipamento;
    private javax.swing.JComboBox cbTipoProblema;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbDeChamado;
    private javax.swing.JLabel lbEquipamento;
    private javax.swing.JLabel lbTipoProblema;
    private javax.swing.JTextArea taDeProblema;
    // End of variables declaration//GEN-END:variables
}
